package com.easygem.system.service.impl;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.system.DeptEmp;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.vo.system.DepartmentVO;
import com.easygem.common.vo.system.EmployeeVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.system.mapper.DeptEmpMapper;
import com.easygem.system.mapper.EmployeeMapper;
import com.easygem.system.service.DepartmentEmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 文晓平
 * @version 1.0
 * @Description 部门业务层的实现
 * @date 2024/12/25 20:53
 */
@Service
public class DeparmentEmpServiceImpl implements DepartmentEmpService {

    @Autowired
    private DeptEmpMapper deptEmpMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 部门列表
     *
     * @param pageNum
     * @param pageSize
     * @param departmentVO
     * @return
     */
    @Override
    public PageVO<DepartmentVO> findDepartmentList(Integer pageNum, Integer pageSize, DepartmentVO departmentVO) {
        Example o = new Example(DeptEmp.class);
        if (departmentVO.getDeptName() != null && !"".equals(departmentVO.getDeptName())) {
            o.createCriteria().andLike("deptName", "%" + departmentVO.getDeptName() + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<DeptEmp> deptEmps = deptEmpMapper.selectByExample(o);
        //转vo
        List<DepartmentVO> departmentVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(deptEmps)) {
            for (DeptEmp department : deptEmps) {
                DepartmentVO d = new DepartmentVO();
                BeanUtils.copyProperties(department, d);
                List<EmployeeVO> employeeVOS = employeeMapper.findEmployeeListByDept(department.getDeptId());
                d.setTotal(employeeVOS.size());
                departmentVOS.add(d);
            }
        }
        PageInfo<DeptEmp> info = new PageInfo<>(deptEmps);
        return new PageVO<>(info.getTotal(), departmentVOS);
    }
    /**
     * 添加院部门
     * @param departmentVO
     */
    @Override
    public DeptEmp add(DepartmentVO departmentVO) {
        DeptEmp deptEmp = new DeptEmp();
        BeanUtils.copyProperties(departmentVO, deptEmp);
        //获得到登录后的用户信息，通用方式
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        deptEmpMapper.insert(deptEmp);
        return deptEmp;
    }

    /**
     * 编辑院部门
     *
     * @param id
     * @return
     */
    @Override
    public DepartmentVO edit(Long id) throws SystemException {
        DeptEmp deptEmp = deptEmpMapper.selectByPrimaryKey(id);
        if(deptEmp ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该编辑的部门不存在");
        }
        DepartmentVO departmentVO = new DepartmentVO();
        BeanUtils.copyProperties(deptEmp,departmentVO);
        return departmentVO;
    }

    /**
     * 更新部门
     *
     * @param id
     * @param departmentVO
     */
    @Override
    public void update(Long id, DepartmentVO departmentVO) throws SystemException {
        DeptEmp dbDeptEmp = deptEmpMapper.selectByPrimaryKey(id);
        if(dbDeptEmp ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新的部门不存在");
        }
        DeptEmp deptEmp = new DeptEmp();
        BeanUtils.copyProperties(departmentVO, deptEmp);
        deptEmp.setDeptId(id);
        deptEmpMapper.updateByPrimaryKeySelective(deptEmp);
    }

    /**
     * 删除院部门
     *
     * @param id
     */
    @Override
    public void delete(Long id) throws SystemException {
        DeptEmp deptClient = deptEmpMapper.selectByPrimaryKey(id);
        if(deptClient ==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的部门不存在");
        }
        deptEmpMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有部门
     *
     * @return
     */
    @Override
    public List<DepartmentVO> findAllVO() {
        List<DeptEmp> deptEmps = deptEmpMapper.selectAll();
        List<DepartmentVO> departmentVOS = new ArrayList<>();
        for (DeptEmp deptEmp : deptEmps) {
            DepartmentVO departmentVO = new DepartmentVO();
            BeanUtils.copyProperties(deptEmp, departmentVO);
            departmentVOS.add(departmentVO);
        }
        return departmentVOS;
    }

    /**
     * 全部部门
     *
     * @return
     */
    @Override
    public List<DeptEmp> findAll() {
        return deptEmpMapper.selectAll();
    }


    /**
     * 根据当前部门编号获得当前部门及以下所有部门信息
     *
     * @return
     */
    @Override
    public DeptEmp findDeptTreeByDeptId() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        DeptEmp deptDb = new DeptEmp();
        deptDb.setDeptId(activeUser.getEmployee().getDeptId());
        DeptEmp dept = deptEmpMapper.selectOne(deptDb);
        return dept;
    }
}
